using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.DataAccess.Service;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.DataAccess.ShardingRule;
using T.FAS.Runtime.DataAccess.SQLDom;
using T.FAS.Runtime.DataAccess.Common;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataModel.Plugin;
using T.FAS.Runtime.DataAccess.Engine;
using System.Threading;
using AppContext = T.ServiceComponent.Session.Service.AppContext;
using Newtonsoft.Json.Linq;

namespace T.FAS.Runtime.DataAccess.UnitTest
{
    [TestClass]
    public class CDOBatchUpdate
    {
        private readonly ChangeSetService _changeService = new ChangeSetService();

        [TestMethod]
        public void TestRefreshTRecord()
        {
            try
            {
                var cdo = TestTool.GetMetaData<CompositeDataObject>("CDO_DO_DEP_UserRequirementWithNoFK", "PresetData", "UserRequirementJson");
                //数据库连接本地TestDatabase
                var @do = TestTool.GetMetaData<DataObject>("DO_DEP_UserRequirement", "PresetData", "UserRequirementJson");
                cdo.BuildModelObjectRelation();
                cdo.RootCompositeNodeObject.DataObject = @do;
                cdo.RootCompositeNodeObject.Associations.ForEach(asso =>
                {
                    if (asso.AssociateType == AssociateType.OuterLeftJoin)
                    {
                        asso.AssoDataObject = new DataObject();
                    }
                });
                cdo.RootCompositeNodeObject.Elements.ForEach(ele =>
                {
                    if (ele.ElementType == ElementType.Normal)
                    {
                        ele.DataColumn = @do.Columns.FirstOrDefault(col => col.ID == ele.DataColumnID);
                    }
                });
                //数据库连接本地TestDatabase
                TRecord newRecord = TestTool.ConstructTRecordBy("UserRequirementData_ChangeSet", "PresetData", "DataJson");
                newRecord.ChangeSet = new TChangeSet
                {
                     ChangeType = TRecordChangeType.Update,
                };
                newRecord.ChangeSet.ChangeSetItems.Add(new TChangeSetItem
                {
                    ColumnCode = "Code",
                    NewValue = "LALLALA",
                });
                var oldRecord = _changeService.RefreshTRecord(cdo, newRecord);
                Assert.IsTrue(oldRecord.GetValue("Code").ToString() == "LALLALA");
            }
            catch (Exception e)
            {

                throw e;
            }
        }
    }
}
